﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'proc_cm_SaveSmallGroup')
BEGIN
    PRINT 'Dropping Procedure proc_cm_SaveSmallGroup'
    DROP  Procedure  proc_cm_SaveSmallGroup
END
GO

PRINT 'Creating Procedure proc_cm_SaveSmallGroup'
GO

CREATE PROCEDURE [dbo].[proc_cm_SaveSmallGroup]
    @pSmallgroupId uniqueidentifier = NULL
    , @pGroupNm nvarchar(255) = NULL
    , @pGroupLeaderId uniqueidentifier = NULL
    , @pGroupLeaderNm nvarchar(50) = NULL
    , @pGroupLeaderEmail varchar(128) = NULL
    , @pGroupLeaderPhone varchar(15) = NULL
    , @pDescriptionTxt nvarchar(max) = NULL
AS
BEGIN
	SET NOCOUNT ON

	IF @pSmallgroupId IS NULL
		SET @pSmallgroupId = NEWID()

	UPDATE 
		[dbo].[tbl_smallgroup]
	SET 
		[group_nm] = @pGroupNm
		,[group_leader_id] = @pGroupLeaderId
		,[group_leader_nm] = @pGroupLeaderNm
		,[group_leader_email] = @pGroupLeaderEmail
		,[group_leader_phone] = @pGroupLeaderPhone
		,[description_txt] = @pDescriptionTxt
	WHERE
		[smallgroup_id] = @pSmallgroupId

	IF @@ROWCOUNT = 0
	BEGIN
		INSERT INTO [dbo].[tbl_smallgroup]
		(
			[smallgroup_id]
			,[group_nm]
			,[group_leader_id]
			,[group_leader_nm]
			,[group_leader_email]
			,[group_leader_phone]
			,[description_txt]
		)
		VALUES
		(
			@pSmallgroupId
			,@pGroupNm
			,@pGroupLeaderId
			,@pGroupLeaderNm
			,@pGroupLeaderEmail
			,@pGroupLeaderPhone
			,@pDescriptionTxt
		)
	END
END
GO

GRANT EXEC ON dbo.proc_cm_SaveSmallGroup TO PUBLIC
GO
